<?php
include '../../../wp-blog-header.php';
$brand_id = '1035';
$cat_id ='3';
$url = 'http://shopping.ebay.in/share/ebaybuzzr.xml';

function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $retValue = curl_exec($ch);
        curl_close($ch);
        return $retValue;
}

$curl_json_output = download_page($url);
$ebay_deals = xml2array($curl_json_output);


//echo '<pre>';
//print_r($ebay_deals);
//echo '</pre>';
//exit();

foreach($ebay_deals['rss']['channel']['item'] as $key => $value){
    $columns_deal['name'] = pods_sanitize($value['Deal_title']);
    $columns_deal['camp_link'] = sanitize_title($columns_deal['name']);
    $ext_img_url = $value['Deal_Image'];
     
    $deal = new Pod('deal');
    $where = "t.camp_link = '".$columns_deal['camp_link']."'";
    $deal->findRecords('id DESC', 1,$where);
    $dealsFound = $deal->getTotalRows();
    //echo $dealsFound; 
    
    if($dealsFound == 0)
    {
      unset($ids);
      unset($current_cities);
      $City = new Pod('city');
    	$City->findRecords('name ASC', -1);
    	while($City->fetchRecord())
    	{
      	$ids  = $City->get_field('id');
      	$current_cities[] = $ids;
    	}
      $current_cities = implode(',',$current_cities);
      $columns_deal['city'] = $current_cities;
      
      $extlink = $value['Destination_URL'];
      $shortened_url = make_bitly_url($extlink);
      if(empty($shortened_url))
      {
        $shortened_url = make_goog_url($extlink);
      }
      if(empty($shortened_url))
      {
        $columns_deal['extlink'] = $value['Destination_URL']; 
      }
      else{
        $columns_deal['extlink'] = $shortened_url;
      }
      $columns_deal['camp_end'] = date('Y-m-d H:i:s',strtotime(str_replace('/','-',$value['End_Date'])));
    //  $columns_deal['camp_end'] = '2011-04-27 07:00:00';
      $columns_deal['camp_start'] = '2011-04-26 00:00:00';
      $columns_deal['brand'] = $brand_id;
      $columns_deal['quickcamp'] = 1;
      $columns_deal['dealcheck'] = 1;
      //$columns_deal['oriprice'] = floor($city_value['COM:value']);
      //$columns_deal['disper'] = floor($city_value['COM:savings']);
      $columns_deal['disprice'] = floor($value['Offer_Price']);
      $columns_deal['ext_image_url'] = $ext_img_url;
      $columns_deal['camp_cat'] = $cat_id;
      
    
      $api = new PodAPI();
      $params = array('datatype' => 'deal', 'columns' => $columns_deal);
      
       
       echo '<pre>';
       print_r($params);
       echo '</pre>';
       //exit();
       
       //$api->save_pod_item($params);
       
     }

}

?>

